package com.haotunet.app.goldtreasure.activity;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.net.Uri;
import android.os.Vibrator;
import android.text.TextUtils;
import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.location.service.LocationService;
import com.baidu.location.service.WriteLog;
import com.baidu.mapapi.SDKInitializer;
import com.haotunet.app.goldtreasure.BuildConfig;
import com.haotunet.app.goldtreasure.net.UrlUtil;
import com.haotunet.app.goldtreasure.utils.Config;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.socialize.PlatformConfig;

import java.util.ArrayList;
import java.util.List;

import cn.tongdun.android.shell.FMAgent;
import cn.tongdun.android.shell.exception.FMException;

public class MyApplication extends Application {
    public LocationService locationService;
    public Vibrator mVibrator;
    private PushAgent mPushAgent;
    public static IWXAPI wxapi;

    @Override
    public void onCreate() {
        super.onCreate();
        ShareStore shareStore = new ShareStore(this);
        String versionName = Config.getVersionName(this);
        shareStore.putString("versionName", versionName);
        shareStore.commit();
      /*  if (BuildConfig.DEBUG) {
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }*/
        ARouter.init(this);
        initTongdun();
        registerWechat();
        mPushAgent = PushAgent.getInstance(this);
        mPushAgent.enable();
        mPushAgent.setDebugMode(true);
        MobclickAgent.setDebugMode(true);
        // SDK在统计Fragment时，需要关闭Activity自带的页面统计，
        // 然后在每个页面中重新集成页面统计的代码(包括调用了 onResume 和 onPause 的Activity)。
        MobclickAgent.openActivityDurationTrack(false);
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_UM_NORMAL);
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {

            @Override
            public void dealWithCustomAction(final Context context, final UMessage msg) {
                if (msg.custom == null) {
                    return;
                }
                final String pushUrl = msg.extra.get("openUrl");
                if (!pushUrl.startsWith("hjb")) {
                    ARouter.getInstance().build("/mine/main").navigation();
                    ARouter.getInstance().build("/mine/invite").withString("pushUrl",pushUrl).navigation();
                } else {
                    List<String> urlList = parseUrl(pushUrl);
                    for (String url : urlList) {
                        Uri uri = Uri.parse(url);
                        ARouter.getInstance().build(uri).navigation();
                    }
                }

            }
        };

        mPushAgent.setNotificationClickHandler(notificationClickHandler);
        locationService = new LocationService(getApplicationContext());
        mVibrator = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        WriteLog.getInstance().init(); // 初始化日志
        SDKInitializer.initialize(getApplicationContext());
    }


    private List<String> parseUrl(String url) {
        ArrayList<String> urlList = new ArrayList<>();
        if (TextUtils.isEmpty(url) && !url.startsWith("hjb")) {
            return urlList;
        }
        Uri uri = Uri.parse(url);
        String param = uri.getQuery();
        String host = uri.getHost();
        List<String> paths = new ArrayList<>();
        paths.add(host);
        paths.addAll(uri.getPathSegments());

        if (!paths.isEmpty()) {
            for (String path : paths) {
                String urlTemp = "";
                switch (path) {
                    case "mine":
                        urlTemp = "hjb:///mine/main";
                        break;
                    case "investment_record":
                        if (TextUtils.isEmpty(param)) {
                            urlTemp = "hjb:///mine/investment_record";
                        } else {
                            urlTemp = "hjb:///mine/investment_record" + "?" + param;
                        }
                        break;
                    case "balance":
                        urlTemp = "hjb:///mine/balance";
                        break;
                    case "redraw_record":
                        urlTemp = "hjb:///mine/redraw_record";
                        break;
                    case "invite":
                        urlTemp = "hjb:///mine/invite" + "?inviteUrl=" + UrlUtil.INVITE_URL;
                        break;
                }
                if (!TextUtils.isEmpty(urlTemp)) {
                    urlList.add(urlTemp);
                }
            }
        }

        return urlList;
    }

    /*初始化同盾*/
    private void initTongdun() {
        try {
            FMAgent.init(this, FMAgent.ENV_PRODUCTION);
        } catch (FMException e) {
            e.printStackTrace();
        }
    }

    private void registerWechat() {
        wxapi = WXAPIFactory.createWXAPI(this, "wx7b4e5d4dec309ac5", true);
        wxapi.registerApp("wx7b4e5d4dec309ac5");
    }


    {
        // 微信 wx12342956d1cab4f9,a5ae111de7d9ea137e88a5e02c07c94d
        PlatformConfig.setWeixin("wx7b4e5d4dec309ac5", "8cd1ea0509b6c9c6292050f8a86190a3");
        // 豆瓣RENREN平台目前只能在服务器端配置
        // 新浪微博
        PlatformConfig.setSinaWeibo("64127917", "a4b62a63274c8cffa23eda94bdd60f8b");

        PlatformConfig.setQQZone("1104910014", "KEYv4cmJMEG6NDrzFX6");
    }
}
